package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.modules.personal.adapter.RelationConcernListAdapter;
import com.yxq.mdt.app.modules.personal.bean.MyRelationBean;
import com.yxq.mdt.app.modules.personal.model.RelationModel;
import com.yxq.mdt.app.modules.personal.presenter.IRelationContract;
import com.yxq.mdt.app.modules.personal.presenter.RelationPresenter;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 关注列表
 */
public class MainPersonalConcernListAc extends BaseActivity<RelationPresenter> implements IRelationContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    RelationConcernListAdapter relationConcernListAdapter;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    int position;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_concern_list;
    }

    @Override
    public void initView() {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.selectMyRelationList(Constant.getLbsUserID(), 2, 1, "", page, 20);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.selectMyRelationList(Constant.getLbsUserID(), 2, 1, "", page, 20);
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        relationConcernListAdapter = new RelationConcernListAdapter(R.layout.layout_item_social_concern_list, null);
        recyclerView.setAdapter(relationConcernListAdapter);
        relationConcernListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mActivity, SocialPersonalHomepageAc.class);
                intent.putExtra("userId", relationConcernListAdapter.getItem(position).getToUserId());
                startActivity(intent);
            }
        });
        relationConcernListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int p) {
                switch (view.getId()) {
                    case R.id.social_attention_tv:
                        position = p;
                        cancelConcern(relationConcernListAdapter.getItem(p).getToUserId());
                        break;
                }
            }
        });
        relationConcernListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        relationConcernListAdapter.isUseEmpty(false);
    }

    /**
     * 取消关注
     *
     * @param id
     */
    private void cancelConcern(String id) {
        mPresenter.saveUsersRelation(id, 1, 0);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new RelationPresenter(this, new RelationModel());
        mPresenter.selectMyRelationList(Constant.getLbsUserID(), 2, 1, "", 1, 20);
    }

    @Override
    public void onLoadSuccess(MyRelationBean myRelationBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();

        tvNum.setText(String.format("我的关注（%d）", myRelationBean.getTotal()));
        if (page == 1) {
            relationConcernListAdapter.setNewData(myRelationBean.getList());
        } else {
            if (myRelationBean.getList() != null && myRelationBean.getList().size() != 0) {
                relationConcernListAdapter.addData(myRelationBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        relationConcernListAdapter.isUseEmpty(!(relationConcernListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onSaveUserRelationSuccess(MyRelationBean myRelationBean) {
        relationConcernListAdapter.remove(position);
    }

    @Override
    public void onSaveUserRelationFailed() {
        ToastUtils.showShort("删除失败");
    }
}
